package org.xiaov.lunasql.engine.function;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import org.xiaov.lunasql.engine.transform.oracle.DateFormatConverter;

/**
 * <p>
 * DateFormat2ToChar
 * </p>
 *
 * @author LunaSQL
 * @since 2024/12/15 13:28
 */
public class DateFormat2ToCharFunc implements MySqlFunction {

    private final static String METHOD_NAME = "date_format";
    private final static String TARGET_METHOD_NAME = "to_char";

    @Override
    public boolean accept(SQLMethodInvokeExpr methodInvokeExpr) {
        return methodInvokeExpr.getMethodName().equalsIgnoreCase(METHOD_NAME);
    }

    @Override
    public void transform(SQLMethodInvokeExpr methodInvokeExpr) {
        methodInvokeExpr.setMethodName(TARGET_METHOD_NAME);
        SQLCharExpr format_mysql = (SQLCharExpr) methodInvokeExpr.getArgument(1);
        SQLCharExpr sqlCharExpr = new SQLCharExpr();
        String format_oracle = DateFormatConverter.mysqlToOracleDateFormat(format_mysql.getText());
        sqlCharExpr.setText(format_oracle);
        methodInvokeExpr.setArgument(1, sqlCharExpr);
    }
}
